import router from "@/router";
import { useLoginUserStore } from "@/stores/user";
import AccessEnum from "./accessEnum";
import checkAccess from "./checkAccess";

router.beforeEach(async (to, from, next) => {
  const loginUserStore = useLoginUserStore();
  let loginUser = loginUserStore.loginUser;
  console.log("loginUser", loginUser);
  const needAccess = (to.meta.access as String) ?? AccessEnum.NOT_LOGIN;
  //要跳转的页面必须要登录
  if (needAccess === AccessEnum.NOT_LOGIN) {
    //如果没有登录 跳转到登录页面
    if(!loginUser ||!loginUser.userRole|| loginUser.userRole === AccessEnum.NOT_LOGIN){
      next(`/user/login?redirect=${to.fullPath}`);
    }
    //如果已经登录 但是权限不足 就跳到无权限页面
    if(!checkAccess(loginUser,needAccess)){
      next('/noAuth')
      return
      }
    }
    next();
})
